package net.xdclass.manager.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.enums.ShortLinkStateEnum;
import net.xdclass.manager.GroupCodeMappingManager;
import net.xdclass.mapper.GroupCodeMappingMapper;
import net.xdclass.model.GroupCodeMappingDO;
import net.xdclass.model.vo.GroupCodeMappingVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class GroupCodeMappingManagerImpl implements GroupCodeMappingManager {
    @Autowired
    private GroupCodeMappingMapper groupCodeMappingMapper;
    @Override
    public GroupCodeMappingDO findByGroupIdAndMappingId(Long groupId, Long accountNo, Long mappingId) {
        return groupCodeMappingMapper.selectOne(new QueryWrapper<GroupCodeMappingDO>()
                .eq("group_id", groupId)
                .eq("account_no", accountNo)
                .eq("id", mappingId));

    }

    @Override
    public int add(GroupCodeMappingDO groupCodeMappingDO) {
        return groupCodeMappingMapper.insert(groupCodeMappingDO);
    }

    @Override
    public int del(String shortLinkCode, Long accountNo, Long groupId) {
        return groupCodeMappingMapper.update(null, new UpdateWrapper<GroupCodeMappingDO>()
                .eq("short_link_code", shortLinkCode)
                .eq("account_no", accountNo)
                .eq("group_id", groupId)
                .set("del", 1));
    }

    @Override
    public Map<String, Object> pageShortLinkByGroupId(int page, int size, Long groupId, Long accountNo) {
        Page<GroupCodeMappingDO> pageInfo = new Page<>(page, size);
        Page<GroupCodeMappingDO> groupCodeMappingDOPage = groupCodeMappingMapper.selectPage(pageInfo, new QueryWrapper<GroupCodeMappingDO>()
                .eq("group_id", groupId)
                .eq("account_no", accountNo)
                .eq("del", 0));
        Map<String, Object> map = new HashMap<>(3);
        map.put("total_record", groupCodeMappingDOPage.getTotal());
        map.put("total_page", groupCodeMappingDOPage.getPages());

        map.put("current_data", BeanUtil.copyToList(groupCodeMappingDOPage.getRecords(), GroupCodeMappingVO.class));
        return map;
    }

    @Override
    public int updateGroupCodeMappingState(String shortLinkCode, ShortLinkStateEnum stateEnum, Long accountNo, Long groupId) {
        return groupCodeMappingMapper.update(null, new UpdateWrapper<GroupCodeMappingDO>()
                .eq("short_link_code", shortLinkCode)
                .eq("account_no", accountNo)
                .eq("group_id", groupId)
                .eq("del", 0)
                .set("state", stateEnum.name()));
    }

    @Override
    public GroupCodeMappingDO findByCodeAndGroupId(String shortLinkCode, Long groupId, Long accountNo) {
        GroupCodeMappingDO groupCodeMappingDO = groupCodeMappingMapper.selectOne(new QueryWrapper<GroupCodeMappingDO>()
                .eq("code", shortLinkCode).eq("account_no", accountNo).eq("del", 0)
                .eq("group_id", groupId));

        return groupCodeMappingDO;
    }

    @Override
    public int update(GroupCodeMappingDO groupCodeMappingDO) {
        int rows = groupCodeMappingMapper.update(null, new UpdateWrapper<GroupCodeMappingDO>()
                .eq("id", groupCodeMappingDO.getId())
                .eq("account_no", groupCodeMappingDO.getAccountNo())
                .eq("group_id", groupCodeMappingDO.getGroupId())
                .eq("del", 0)

                .set("title", groupCodeMappingDO.getTitle())
                .set("domain", groupCodeMappingDO.getDomain())
        );

        return rows;
    }

    @Override
    public int del(GroupCodeMappingDO groupCodeMappingDO) {
        int rows = groupCodeMappingMapper.update(null, new UpdateWrapper<GroupCodeMappingDO>()
                .eq("id", groupCodeMappingDO.getId())
                .eq("account_no", groupCodeMappingDO.getAccountNo())
                .eq("group_id", groupCodeMappingDO.getGroupId())

                .set("del", 1)
        );

        return rows;
    }

    private GroupCodeMappingVO beanProcess(GroupCodeMappingDO groupCodeMappingDO) {
        GroupCodeMappingVO groupCodeMappingVO = new GroupCodeMappingVO();
        BeanUtils.copyProperties(groupCodeMappingDO, groupCodeMappingVO);

        return groupCodeMappingVO;
    }
}
